#include "PlatformProperty.h"
// [SW_FC_009_013] 2.外后视镜手动折叠一键折叠
SYSTEM_READ_WRITE_PROPERTY(MIRROR_FOLD, bool) {
    builder.area(VehicleArea::GLOBAL)
        .initValue([]() { return false; })
        .withStatusBySignals(
            []([[maybe_unused]] PropertyStatus lastStatus) {
                uint32_t lcfg = lcfg::LocalConfig::getInstance().getInt<int32_t>(
                    lcfg::LCFG_FoldExteriorMirror_SoftSwt_Btn);
                auto lcfgVehSpd = lcfg::LocalConfig::getInstance().getInt<int32_t>(
                    lcfg::LCFG_SpeedForExteriorRearMirrorFold);
                auto carmod = SIGNAL.getVehModMngtGlbSafe1CarModSts1();
                auto usgmod = SIGNAL.getVehModMngtGlbSafe1UsgModSts();
                bool mirrFoldStsAtPassTimeout =
                    SIGNAL.getMirrFoldStsAtPassTimeout() == sdb::Timeout::Timeout;
                bool mirrFoldStsAtDrvrTimeout =
                    SIGNAL.getMirrFoldStsAtDrvrTimeout() == sdb::Timeout::Timeout;
                auto cc156 = SIGNAL.getcarconfig156();
                float VehSpdLgtA = SIGNAL.getVehSpdLgtA() * 3.6;
                VHALOG.d(
                    "MIRROR_FOLD status  Change carmod[%d], usgmod[%d], "
                    "cc156[%d],mirrFoldStsAtPassTimeout=%d,getMirrFoldStsAtDrvrTimeout=%d, vehSpdLgtA=%f, lcfg=%d, lcfgVehspd=%d",
                    carmod, usgmod, cc156, SIGNAL.getMirrFoldStsAtPassTimeout(),
                    SIGNAL.getMirrFoldStsAtDrvrTimeout(), VehSpdLgtA, lcfg, lcfgVehSpd);
                if (SIGNAL.carConfigIs(SignalRX::carconfig156, sdb::CarConfig156::_02,
                                       sdb::CarConfig156::_03, sdb::CarConfig156::_81,
                                       sdb::CarConfig156::_82) &&
                    lcfg == 1) {
                    if (SIGNAL.carModeIs(sdb::CarModSts1::CarModFcy, sdb::CarModSts1::CarModTrnsp,
                                         sdb::CarModSts1::CarModCrash, sdb::CarModSts1::CarModDyno,
                                         sdb::CarModSts1::CarModNorm) &&
                        SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModInActv,
                                         sdb::UsgModSts1::UsgModCnvinc, sdb::UsgModSts1::UsgModActv,
                                         sdb::UsgModSts1::UsgModDrvg) && VehSpdLgtA < lcfgVehSpd) {
                        if (mirrFoldStsAtPassTimeout || mirrFoldStsAtDrvrTimeout) {
                            VHALOG.d("MIRROR_FOLD withStatusBySignals: Error");
                            return PropertyStatus::Error;
                        } else {
                            VHALOG.d("MIRROR_FOLD withStatusBySignals: Active");
                            return PropertyStatus::Active;
                        }
                    } else {
                        VHALOG.d("MIRROR_FOLD withStatusBySignals: NotActive");
                        return PropertyStatus::NotActive;
                    }
                } else {
                    VHALOG.d("MIRROR_FOLD withStatusBySignals: NotAvailable");
                    return PropertyStatus::NotAvailable;
                }
            },
            SignalRX::carconfig156, SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1, SignalRX::MirrFoldStsAtPassTimeout,
            SignalRX::MirrFoldStsAtDrvrTimeout,SignalRX::VehSpdLgtA)
        .withValueBySignals(
            []([[maybe_unused]] bool last) {
                VHALOG.d("MIRROR_FOLD : last[%d] ", last);
                bool mirrFoldStsAtPassTimeout =
                    SIGNAL.getMirrFoldStsAtPassTimeout() == sdb::Timeout::Timeout;
                bool mirrFoldStsAtDrvrTimeout =
                    SIGNAL.getMirrFoldStsAtDrvrTimeout() == sdb::Timeout::Timeout;
                if (mirrFoldStsAtPassTimeout || mirrFoldStsAtDrvrTimeout) {
                    VHALOG.d("MIRROR_FOLD : timeout, return last[%d]", last);
                    return last;
                }
                sdb::MirrFoldStsTyp drvr = SIGNAL.getMirrFoldStsAtDrvr();
                sdb::MirrFoldStsTyp pass = SIGNAL.getMirrFoldStsAtPass();
                if (drvr == sdb::MirrFoldStsTyp::MirrMovgToFold ||
                    drvr == sdb::MirrFoldStsTyp::MirrFoldPosn ||
                    pass == sdb::MirrFoldStsTyp::MirrMovgToFold ||
                    pass == sdb::MirrFoldStsTyp::MirrFoldPosn) {
                    VHALOG.d("MIRROR_FOLD : cur[true] ");
                    //更新开关状态fold
                    return true;
                } else if (drvr == sdb::MirrFoldStsTyp::MirrMovgToNotFold ||
                    drvr == sdb::MirrFoldStsTyp::MirrNotFoldPosn ||
                    pass == sdb::MirrFoldStsTyp::MirrMovgToNotFold ||
                    pass == sdb::MirrFoldStsTyp::MirrNotFoldPosn) {
                    VHALOG.d("MIRROR_FOLD : cur[false] ");
                    //更新开关状态unfold
                    return false;
                } else {
                    VHALOG.d("MIRROR_FOLD : MirrFoldStsAtDrvr or MirrFoldStsAtPass return 0, cur[last] ");
                    return last;
                }
            },
            SignalRX::MirrFoldStsAtDrvr,
            SignalRX::MirrFoldStsAtPass,
            SignalRX::MirrFoldStsAtPassTimeout,
            SignalRX::MirrFoldStsAtDrvrTimeout)
            .onSetProperty([](const bool value) {
                VHALOG.d("halOnSetProperty MIRROR_FOLD : %d", value);
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                     VHALOG.d("MIRROR_FOLD : setCB_VFC_MirrCtrlForHmiCen");
                     SIGNAL.setCB_VFC_MirrCtrlForHmiCen(sdb::OnOff1::On);
                }
                SIGNAL.setExtrMirrFoldHmiReq(sdb::PsdNotPsd1::Psd);
            });
}